package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
@ResponseBody
public class HouseController {
    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private UserFollowService userFollowService;

    @DubboReference
    private AdminService adminService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum,
                       @PathVariable Integer pageSize,
                       @RequestBody HouseQueryVo houseQueryVo) {
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum, pageSize, houseQueryVo);
        return Result.ok(pageInfo);
    }

    @RequestMapping("/info/{houseId}")
    public Result info(@PathVariable Long houseId, HttpSession session) {
        House house = houseService.getById(houseId);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.getByHouseId(houseId);
        if (houseBrokerList == null || houseBrokerList.size() == 0) {
            Admin admin = adminService.getById(1);
            HouseBroker houseBroker = new HouseBroker();
            houseBroker.setHouseId(houseId);
            houseBroker.setBrokerId(admin.getId());
            houseBroker.setBrokerName(admin.getName());
            houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
            houseBrokerList.add(houseBroker);
        }
        List<HouseImage> houseImage1List = houseImageService.getByHouseId(houseId, 1);
        Boolean isFollow = false;
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        if (userInfo != null) {
            UserFollow userFollow = userFollowService.getByUserIdAndHouseId(userInfo.getId(),houseId);
            if (userFollow != null) {
                isFollow = true;
            }
        }

        Map map = new HashMap();
        map.put("house", house);
        map.put("community", community);
        map.put("houseBrokerList", houseBrokerList);
        map.put("houseImage1List", houseImage1List);
        map.put("isFollow", isFollow);
        return Result.ok(map);
    }
}
